package com.erdos.message.vo;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;

import com.thoughtworks.xstream.annotations.XStreamOmitField;

public abstract class AbstractL2RHMessage implements L2Messaging, XmlNameSpaceSupport{
	private static final long serialVersionUID = 7067491702091406436L;
	private static final String messageProvider=L2Messaging.class.getSimpleName();
	private static final String xmlVersionDeclaration="<?xml version=\"1.0\" encoding=\"utf-8\"?>";
	
	private static final String baseNamespaceUri = "http://www.w3.org/2001/XMLSchema-instance";
	private static final String baseSchemaLocationUri = " ";
	/**
     * default prefix for all L2 message
     */
    private static final String PREFIX="";
    /**
     * default name space front part for L2 message
     */
    private static final String NAMESPACE="xmlns";
    /**
     * default schema location front part for L2 message
     */
    private static final String SCHEMALOCATION="schemaLocation";
    /**
     * default xsi front part for L2 message
     */
    private static final String XSI="xsi";
	/**
	 * message header which holds the non-body information
	 */
	private L2RHMessageHeader messageHeader;
	
	@XStreamOmitField
	private String messageId;
	
	public AbstractL2RHMessage() {
		this.messageHeader=new L2RHMessageHeader();
		this.messageHeader.setMessageTypeId(this.getMessageTypeId());
	}
	
	/**
	 * @return the messageHeader
	 */
	public L2RHMessageHeader getMessageHeader() {
		return messageHeader;
	}

	/**
	 * @param messageHeader the messageHeader to set
	 */
	public void setMessageHeader(L2RHMessageHeader messageHeader) {
		this.messageHeader = messageHeader;
	}



	/* (non-Javadoc)
	 * @see com.hp.message.vo.L2Messaging#getMessageId()
	 */
	public String getMessageId() {
		return this.messageId;
	}

	public void setMessageId(String msgId) {
		this.messageId = msgId;
	}
	
	public String getMessageProvider() {
		return messageProvider;
	}

	/**
     * default prefix string.
     */
    public String getPrefix() {
        return PREFIX;
    }

	public String getXmlVersionDeclaration() {
        return xmlVersionDeclaration;
    }

	/**
     * default name space string.
     */
    public final String getNameSpace() {
        String nsUri=this.getNameSpaceUri();
        String slUri=this.getSchemaLocationUri();
        StringBuilder ns=new StringBuilder("");
        if(StringUtils.isNotEmpty(nsUri)) {         
            ns.append(NAMESPACE).append(":").append(XSI).append("=\"").append(nsUri).append("\"")
            .append(" ").append(XSI).append(":").append(SCHEMALOCATION).append("=\"").append(slUri).append("\"");             
        }

        return ns.toString();
    }

    /**
     * for subclasses to extend to supply the name space URI.
     * @return URI of the name space
     * @author zxu
     * @since  2008-12-2
     */
    protected String getNameSpaceUri() {
        return baseNamespaceUri;
    }
    
    /**
     * for subclasses to extend to supply the schema location URI.
     * @return URI of the schema location
     * @author zxu
     * @since  2008-12-2
     */
    protected String getSchemaLocationUri() {
        return baseSchemaLocationUri;
    }

	public String toString() {
		return new ToStringBuilder(this).append(this.messageHeader).toString();
	}
	
	/* (non-Javadoc)
	 * @see com.hp.message.vo.L2Messaging#getReceiver()
	 */
	public String getReceiver() {		
		return this.messageHeader.getReceiver();
	}

	/* (non-Javadoc)
	 * @see com.hp.message.vo.L2Messaging#getSender()
	 */
	public String getSender() {
		return this.messageHeader.getSender();
	}	
}
